<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class ArticlecateController extends Controller
{
    //打开数据添加页面
    public function getAdd()
    {   
        $articlecates=DB::table('articlecate')->select(DB::raw('*, concat(path,",",id) as pd'))->orderBy('pd')->get();
        return view('admin.articlecate.add',['articlecates'=>$articlecates]);
    }
    //数据插入
    public function postInsert(Request $request)
    {
        //获取数据
        $data=$request->except('_token');
        if(empty($data['name'])){
            return back()->with('error','分类名称不可为空');
        }
        $p=DB::table('articlecate')->where('id','=',$data['pid'])->first();
        // dd($p);
        if(!$p){
            $data['path']='0';
        }else{
            $data['path']=$p->path.','.$p->id;
        }
        //数据写入数据库
        $res=DB::table('articlecate')->insert($data);
        if($res){
            return redirect('articlecate/list')->with('info','添加分类成功');
        }else{
            return back()->with('error','添加分类失败');
        }
    }
    //列表显示
    public function getList(Request $request)
    { 
        $articlecates=DB::table('articlecate')->
            select(DB::raw('*, concat(path,",",id) as pd'))->
            orderBy('pd')->get();
            // var_dump($articlecates);die;
        foreach($articlecates as $k=>$value){
            $p = DB::table('articlecate')->select('name')->where('id','=',$value->pid)->first();
            if($p){
                $value->pid = $p->name;
            }else{
                $value->pid = '顶级分类';
            }
            if($value->path != '0'){
                $arr=explode(",",$value->path);
                $value->path='';
                foreach($arr as $v){
                    // global $value;
                    $j=DB::table('articlecate')->select('name')->where('id','=',$v)->first();
                    if($j){
                        $value->path .= $j->name.'/';
                    }else{
                        $value->path .= '顶级分类/';
                    }
                }
            }else{
                $value->path= '顶级分类/';
            }

            $num=substr_count($value->path,'/');
            $value->name=str_repeat('|----',$num-1).$value->name;
            if($value->status == 1){
                $value->status ='正常';
            }else{
                $value->status ='冻结';
            }
        }
        // dd($articlecates);
        return view('admin.articlecate.list',['articlecates'=>$articlecates,'request'=>$request]);
    }
    //跳转到更改数据页面
    public function getEdit(Request $request)
    {
        $id=$request->input('id');
        $articlecate=DB::table('articlecate')->where('id',$id)->first();
        $articlecates=DB::table('articlecate')->select(DB::raw('*, concat(path,",",id) as pd'))->orderBy('pd')->get();
        // var_dump($articlecate);
        return view('admin.articlecate.edit',['articlecate'=>$articlecate,'articlecates'=>$articlecates]);
    }
    //信息修改
    public function postUpdate(Request $request)
    {
        //获取修改值
        $data=$request->except(['_token','id']);
        //修改值判定
        if(empty($data['name'])){
            return back()->with('error','分类名称不可为空');
        }
        //获取父级分类的信息
        $p=DB::table('articlecate')->where('id','=',$data['pid'])->first();
        // dd($p);
        //拼接路径信息
        if(!$p){
            $data['path']='0';
        }else{
            $data['path']=$p->path.','.$p->id;
        }
        //数据修改至数据库
        $res=DB::table('articlecate')->where('id',$request->input('id'))->update($data);
        if($res){
            return redirect('articlecate/list')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
    //信息删除
    public function getDrop(Request $request)
    {
        $id=$request->input('id');
        $data=DB::table('articlecate')->where('id',$id)->first();
        $res=DB::table('articlecate')->where('id',$id)->orWhere('path','like',$data->path.','.$id.'%')->delete();
        if($res){
            return redirect('articlecate/list')->with('info','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }
}
